package com.midea.ai.air.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.view.View;

import com.midea.ai.air.R;

/**
 * Created by ouyangdi on 2016/3/9.
 * 虚线View
 */
public class DashLineView extends View{
    private Paint mPaint; //画笔
    private Path mPath;
    private float mIntervalLine;
    private float mIntervalEmpty;

    public DashLineView(Context context) {
        this(context, null, 0);
    }

    public DashLineView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DashLineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.DashLineView);
        int color = array.getColor(R.styleable.DashLineView_line_color, getResources().getColor(R.color.line_gray));
        array.recycle();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(color);
        mPaint.setAntiAlias(true);

        mIntervalEmpty = getResources().getDimension(R.dimen.height_4dp);
        mIntervalLine = getResources().getDimension(R.dimen.height_2dp);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawPath(mPath, mPaint);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mPath = new Path();
        mPath.moveTo(0, h/2);
        mPath.lineTo(w, h/2);
        PathEffect effects = new DashPathEffect(new float[]{mIntervalLine,mIntervalEmpty},1);
        mPaint.setPathEffect(effects);
        mPaint.setStrokeWidth(h);
    }
}
